from mysql.connector import pooling

class DBMgr():
    __slots__ = ('__pool')

    @classmethod
    def setPool(cls, conf):
        cls.__pool = pooling.MySQLConnectionPool(pool_name="mypool", pool_size=10, connection_timeout=30, **conf)

    class ConnectionContext:
        __slots__ = ('__connection')

        def __init__(self, connection):
            self.__connection = connection

        def __enter__(self):
            return self.__connection
    
        def __exit__(self, exc_type, exc_value, exc_trace):
            if self.__connection:
                self.__connection.close()
                self.__connection = None

    @classmethod
    def getConnect(cls):
        return cls.ConnectionContext(cls.__pool.get_connection())